
const getMysql = require('../mysql')

// 当前登录状态信息查询
var getUser = async (req, res) => {
    let uname = req.body.uname
    let sql = 'select uname,iphone,avatar,gender,email,qianming,lv,exp from tarvel_user where uname = ?'
    getMysql(sql, uname).then(result => {
        res.send({
            code: 200,
            message: '查询成功!!',
            data: result
        })
    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败!!',
        })
    })
}



// 头像上传
var myinfo = (req, res) => {
    // console.log(req.files);
    let timer = +new Date()
        let names = timer + "." + req.files.file.name.split('.')[1]
        if (!req.files || Object.keys(req.files).length === 0) {
            return res.status(400).send('ok');
        }
        let sampleFile = req.files.file;
        // 将路径做处理后返回给前端
        let uploadPath = __dirname + '/public/' +names
        let myAvater = uploadPath.split('/').slice(-1)[0]
        // console.log(myAvater);
        sampleFile.mv(uploadPath, function (err) {
            if (err) return res.status(500).send(err);
            res.send({
                code: 200,
                data: `http://127.0.0.1:3000/router/public/${myAvater}`
            });
        });
};


// 上传成功后调用接口更新数据库
const updataAva = async (req, res) => {
    let uname = req.body.uname;
    let avatar = req.body.avatar;
    let sql = 'update tarvel_user set avatar = ? where uname = ?'
    getMysql(sql, avatar, uname).then(result => {
        res.send({
            code: 200,
            message: '保存成功！'
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '保存失败！'
        })
    })

}

// 更新用户信息
var updataUser = async (req, res) => {
    let gender = req.body.gender
    let email = req.body.email
    let qianming = req.body.qianming
    let uname = req.body.uname
    let sql = 'update tarvel_user set gender=?, email=?,qianming=? where uname = ?'
    getMysql(sql, gender, email, qianming, uname).then(result => {
        res.send({
            code: 200,
            message: '更新成功！'
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '保存失败！'
        })
    })

}


// 用户收藏游记帖子
const collectInfo = async (req, res) => {
    let username = req.query.username
    let sql = 'select * from community_collect where username = ?'
    getMysql(sql, username).then(result => {
        res.send({
            code: 200,
            message: '查询成功！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 200,
            message: '查询失败',
        })
    })
}


// 用户收藏酒店帖子
const collectInfoHotel = async (req, res) => {
    let username = req.query.username
    let sql = 'select * from hotel_collect where username = ?'
    getMysql(sql, username).then(result => {
        res.send({
            code: 200,
            message: '查询成功！',
            data: result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 200,
            message: '查询失败',
        })
    })
}




// 做任务，更新经验值
var updataexpIstrue = async (req, res) => {
    let exp = req.body.exp
    let uname = req.body.uname  
    let sql = 'update tarvel_user set exp = exp+? where uname = ?'
    getMysql(sql, exp, uname).then(result => {
        res.send({
            code: 200,
            message: '经验值更新成功！',
        })
    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '更新失败！'
        })
    })
}


// 经验满 等级升级，经验条清零
var expIszieo = async (req, res) => {
    let uname = req.body.uname
    let sql = 'update tarvel_user set exp = 0 where uname = ?'
    let sql2 = 'update tarvel_user set lv=lv+1 where uname = ?'
    getMysql(sql, uname).then(result => {
        getMysql(sql2, uname).then(count => {
            res.send({
                code: 200,
                message: '经验值清零成功,用户等级提升',
            })
        })

    }).catch(err => {
        console.log(err);
        res.send({
            code: 201,
            message: '更新失败！'
        })
    })

}





module.exports = {
    myinfo,
    getUser,
    updataAva,
    updataUser,
    collectInfo,
    expIszieo,
    updataexpIstrue,
    collectInfoHotel
}